﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Pechkin;

namespace ZBJF.Warranty.Common.Common
{
    public class PechkinPDF
    {
        ///  <summary>
        ///  发布需要把libeay32.dll，libgcc_s_dw2-1.dll,mingwm10.dll,ssleay32.dll,wkhtmltox0.dll 复制到bin目录，应用程序池高级设置中必须选择启用32位应用程序
        /// 转为bytes后就可以下载pdf文件了，比如MVC:return File(buf, "application/pdf",filename
        /// 或者Response.ContentType = "application/octet-stream";
        /// 通知浏览器下载文件而不是打开
        /// Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
        /// Response.BinaryWrite(buf);
        /// Response.Flush();
        /// Response.End(););
        ///  </summary>
        ///  <param name="url"></param>
        /// <param name="minFontSize"></param>
        /// <returns></returns>
        public byte[] ConvertUrlToBytes(string url, double minFontSize = 0)
        {
            using (IPechkin pechkin = Factory.Create(new GlobalConfig()))
            {
                ObjectConfig oc = new ObjectConfig();
                oc.SetPrintBackground(true)
                    .SetLoadImages(true)
                    .SetScreenMediaType(true).SetIncludeInOutline(true)
                    .SetPageUri(url);
                if (minFontSize > 0)
                    oc.SetMinFontSize(minFontSize);
                return pechkin.Convert(oc);
            }
        }
    }
}
